﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using CSKLiteWindowsPhone.Common;
using Coding4Fun.Phone.Controls;

namespace CSKLiteWindowsPhone
{
    public partial class Settings : PhoneApplicationPage
    {
        List<Enviroment> enviroment = new List<Enviroment>();
        public Settings()
        {
            InitializeComponent();
            this.InitData();
            this.Loaded += new RoutedEventHandler(Settings_Loaded);
            this.listEnviroment.SelectionChanged += new SelectionChangedEventHandler(listEnviroment_SelectionChanged);

        }

        void listEnviroment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.listEnviroment.SelectedItem != null)
            {
                IsolatedStorageSettings.ApplicationSettings.Remove(Constants.EnviromentKey);
                IsolatedStorageSettings.ApplicationSettings[Constants.EnviromentKey] = this.listEnviroment.SelectedItem;

                if (e.AddedItems.Count > 0 && e.RemovedItems.Count > 0)
                {
                    MessagePrompt mp = new MessagePrompt()
                    {
                        Body = "Re-login is required"
                    };
                    mp.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(mp_Completed);
                    mp.Show();
                }
            }
        }

        void mp_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
            });   
        }

        void InitData()
        {
            this.enviroment.Clear();
            if (IsolatedStorageSettings.ApplicationSettings.Contains(Constants.EnviromentKey))
            {
                this.enviroment.Add(IsolatedStorageSettings.ApplicationSettings[Constants.EnviromentKey] as Enviroment);
            }

            if (!this.IsItemAdded(Constants.QA))
            {
                this.enviroment.Add(new Enviroment(Constants.QA, "http://www.qa.badgroup.com/services/websitedata.asmx"));
            }

            if (!this.IsItemAdded(Constants.Dev))
            {
                this.enviroment.Add(new Enviroment(Constants.Dev, "http://www.dev.badgroup.com/services/websitedata.asmx"));
            }

            if (!this.IsItemAdded(Constants.Prod))
            {
                this.enviroment.Add(new Enviroment(Constants.Prod, "http://www.websense.com/services/websitedata.asmx"));
            }
            this.listEnviroment.ExpansionMode = ExpansionMode.ExpansionAllowed;
            this.listEnviroment.SelectionMode = SelectionMode.Single;
            this.listEnviroment.Items.Clear();
            this.listEnviroment.ItemsSource = this.enviroment;
        }

        bool IsItemAdded(string key)
        {
            foreach (var item in this.enviroment)
            {
                if (item.Name == key)
                {
                    return true;
                }
            }

            return false;
        }

        void Settings_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
    }

    public class Enviroment
    {
        public string Name { get; set; }
        public string Value { get; set; }

        public Enviroment(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }
    }
}